package org.video.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import org.greenrobot.eventbus.EventBus;
import org.video.mine.GlobalStr;
import org.video.mine.R;
import org.video.mine.databinding.LoadedDetailLayoutBinding;
import org.video.mine.databinding.LoadedLayoutBinding;
import org.video.mine.databinding.LoadingProgressBinding;
import org.video.mine.entity.LoadedEntity;
import org.video.mine.entity.ProgressLoadEntity;
import org.video.mine.event.DeleteEditEvent;
import org.video.mine.model.ClickModel;

import java.util.List;

public class LoadDetailAdapter extends RecyclerView.Adapter<GlobalStr.ViewHolder> {

    private List<LoadedEntity> loadedEntities;
    private Context context;

    public LoadDetailAdapter(List<LoadedEntity> loadedEntities, Context context) {
        this.loadedEntities = loadedEntities;
        this.context = context;
    }

    @NonNull
    @Override
    public GlobalStr.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.loaded_detail_layout, parent, false);
        return new GlobalStr.ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull GlobalStr.ViewHolder holder, final int position) {
        LoadedEntity loadedEntity = loadedEntities.get(position);
        LoadedDetailLayoutBinding binding = ((LoadedDetailLayoutBinding)holder.getTitleBinding());
        binding.setClick(new ClickModel());
        binding.setConetxt(context);
        binding.setLoadinfo(loadedEntity);
        binding.cbDeleteChoose.setChecked(loadedEntity.isCheck());
        if (loadedEntity.isEdit()){
            binding.cbDeleteChoose.setVisibility(View.VISIBLE);
        }else {
            binding.cbDeleteChoose.setChecked(false);
            binding.cbDeleteChoose.setVisibility(View.GONE);
        }
        binding.cbDeleteChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new DeleteEditEvent(position));
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull GlobalStr.ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()){
            super.onBindViewHolder(holder, position, payloads);
            return;
        }
        LoadedEntity loadedEntity = loadedEntities.get(position);
        //循环得到payloads里面的参数
        LoadedDetailLayoutBinding binding = ((LoadedDetailLayoutBinding)holder.getTitleBinding());
        for (Object payload:payloads) {
            switch (String.valueOf(payload)){
                case "check":
                    binding.cbDeleteChoose.setChecked(loadedEntity.isCheck());
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return loadedEntities.size();
    }
}
